﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DungeonGenerator;

public class TileMap : MonoBehaviour {

    public GameObject grid;
    public Sprite[] mapTile;
    SpriteRenderer mapSprite;
    public int level;
    public List<GameObject> node;
    Node vertex;
    //GraphData graphData;
    public bool done = false;
    public GameObject player;
    List<Node> graph;
    public GameObject nodeObject;

	// Use this for initialization
	void Start() 
    {
        Generate();
	}

    //void ShowDestination()
    //{
    //    Debug.Log("--------abis dijkstra gan--------");
    //    foreach (var i in graphData.Nodes)
    //    {
    //        Debug.Log("node: " + i.Id + " weight: " + i.Weight);
            
    //        if (i.Previous != null)
    //        {
    //            Debug.Log("node " + i.Id + " visited gan");
                
    //        }
    //        Debug.Log("distance: " + i.Distance);
    //    }
    //}
	

    // Update is called once per frame
    void Update () 
    {
        if (done)
        {
            //if (player.transform.position.x < )
            //{
            //    playerSpawner.transform.Translate(Vector3.right * moveSpd);
            //}
            //if (playerLoc.y < targetPos.y)
            //{
            //    playerSpawner.transform.Translate(Vector3.up * moveSpd);
            //}
            //if (playerLoc.x > targetPos.x)
            //{
            //    playerSpawner.transform.Translate(Vector3.right * -moveSpd);
            //}
            //if (playerLoc.y > targetPos.y)
            //{
            //    playerSpawner.transform.Translate(Vector3.up * -moveSpd);
            //}
        }
	
    }

    void OnMouseDown() 
    {
        done = true;
    }

    void InitGridMap()
    {
       
    }

    void Generate()
    {
        node = new List<GameObject>();
        IService service = new MockService();

        EpicMapSingleton epicMapSingleton = EpicMapSingleton.CreateSingleton();
        level = epicMapSingleton.Player.LevelMax;
        Debug.Log("level saya " + epicMapSingleton.Player.LevelMax);
        
        
        GraphData graphData = GraphData.CreateGraphSingleton();
        BaseMapDirector mapDirector=new BaseMapDirector();
        BaseMapBuilder mapBuilder = null;

        if (level <= 10)
        {
            mapBuilder = new MapLevel1();
            mapDirector.construct(mapBuilder);

            graph = new List<Node>();
            foreach (Node a in mapBuilder.levelMap.Nodes)
            {
                a.Previous = new Node();
                graph.Add(a);
                graphData.Nodes.Add(a);
                Debug.Log(a.Id + " weight: " + a.Weight);
                GameObject o = Instantiate(nodeObject, a.Position, transform.rotation) as GameObject;
                o.GetComponent<Battle>().monsterLawan = a.Weight;
                if (a.Id == 1)
                {
                    o.GetComponent<SpriteRenderer>().sprite = mapTile[5];
                }
                else if (a.Id == mapBuilder.levelMap.Nodes.Count)
                {
                    o.GetComponent<SpriteRenderer>().sprite = mapTile[0];
                }
                else
                    o.GetComponent<SpriteRenderer>().sprite = mapTile[Random.Range(1, 5)];
                

            }
        } 
        else
            if (level > 10 && level <= 20)
            {
                mapBuilder = new MapLevel11();
                mapDirector.construct(mapBuilder);

                graph = new List<Node>();
                foreach (Node a in mapBuilder.levelMap.Nodes)
                {
                    a.Previous = new Node();
                    graph.Add(a);
                    graphData.Nodes.Add(a);
                    Debug.Log(a.Id + " weight: " + a.Weight);
                    GameObject o = Instantiate(nodeObject, a.Position, transform.rotation) as GameObject;
                    o.GetComponent<Battle>().monsterLawan = a.Weight;
                    if (a.Id == 1)
                    {
                        o.GetComponent<SpriteRenderer>().sprite = mapTile[5];
                    }
                    else if (a.Id == mapBuilder.levelMap.Nodes.Count)
                    {
                        o.GetComponent<SpriteRenderer>().sprite = mapTile[0];
                    }
                    else
                        o.GetComponent<SpriteRenderer>().sprite = mapTile[Random.Range(1, 5)];

                }
            }
            else
                if (level > 20 && level <=30)
                {
                    mapBuilder = new MapLevel21();
                    mapDirector.construct(mapBuilder);

                    graph = new List<Node>();
                    
                    foreach (Node a in mapBuilder.levelMap.Nodes)
                    {
                        a.Previous = new Node();
                        graph.Add(a);
                        graphData.Nodes.Add(a);
                        Debug.Log(a.Id + " weight: " + a.Weight);
                        GameObject o = Instantiate(nodeObject, a.Position, transform.rotation) as GameObject;
                        o.GetComponent<Battle>().monsterLawan = a.Weight;
                        if (a.Id == 1)
                        {
                            o.GetComponent<SpriteRenderer>().sprite = mapTile[5];
                        }
                        else if (a.Id == mapBuilder.levelMap.Nodes.Count)
                        {
                            o.GetComponent<SpriteRenderer>().sprite = mapTile[0];
                        }
                        else
                            o.GetComponent<SpriteRenderer>().sprite = mapTile[Random.Range(1, 5)];


                    }
                }
                else
                    if (level > 30 && level <= 40)
                    {
                        mapBuilder = new MapLevel31();
                        mapDirector.construct(mapBuilder);

                        graph = new List<Node>();
                        
                        foreach (Node a in mapBuilder.levelMap.Nodes)
                        {
                            a.Previous = new Node();
                            graph.Add(a);
                            graphData.Nodes.Add(a);
                            Debug.Log(a.Id + " weight: " + a.Weight);
                            GameObject o = Instantiate(nodeObject, a.Position, transform.rotation) as GameObject;
                            o.GetComponent<Battle>().monsterLawan = a.Weight;
                            if (a.Id == 1)
                            {
                                o.GetComponent<SpriteRenderer>().sprite = mapTile[5];
                            }
                            else if (a.Id == mapBuilder.levelMap.Nodes.Count)
                            {
                                o.GetComponent<SpriteRenderer>().sprite = mapTile[0];
                            }
                            else
                                o.GetComponent<SpriteRenderer>().sprite = mapTile[Random.Range(1, 5)];

                        }
                    }
                    else
                        if (level > 40 && level <= 50)
                        {
                            mapBuilder = new MapLevel41();
                            mapDirector.construct(mapBuilder);

                            graph = new List<Node>();
                            foreach (Node a in mapBuilder.levelMap.Nodes)
                            {
                                a.Previous = new Node();
                                graph.Add(a);
                                graphData.Nodes.Add(a);
                                Debug.Log(a.Id + " weight: " + a.Weight);
                                GameObject o = Instantiate(nodeObject, a.Position, transform.rotation) as GameObject;
                                o.GetComponent<Battle>().monsterLawan = a.Weight;
                                if (a.Id == 1)
                                {
                                    o.GetComponent<SpriteRenderer>().sprite = mapTile[5];
                                }
                                else if (a.Id == mapBuilder.levelMap.Nodes.Count)
                                {
                                    o.GetComponent<SpriteRenderer>().sprite = mapTile[0];
                                }
                                else
                                    o.GetComponent<SpriteRenderer>().sprite = mapTile[Random.Range(1, 5)];


                            }
                        }

        Dijkstra jikstra = new Dijkstra(graphData.Nodes[0]);

        foreach (Node i in graphData.Nodes)
        {
            Debug.Log(i.Id + ": " + i.Previous.Id);
        }

        int j = graphData.Nodes.Count - 1;
        while (j != 0)
        {
            graphData.Nodes[j].IsSelected = true;
            j = graphData.Nodes[j].Previous.Id - 1;
            
        }
        graphData.Nodes[0].IsSelected = true;

        foreach (Node i in graphData.Nodes)
        {
            Debug.Log("Node " + i.Id + ": " + i.IsSelected);
        }
    }
}